package com.scau402.system1.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scau402.system1.domain.po.WmsAdjustBoundOrderDetailPO;
import com.scau402.system1.domain.po.WmsAdjustPriceOrderDetailPO;
import com.scau402.system1.domain.po.WmsAuditOrderPO;
import com.scau402.system1.domain.query.AuditOrderQuery;
import com.scau402.system1.domain.query.WmsAdjustBoundOrderQuery;
import com.scau402.system1.domain.vo.WmsAdjustBoundOrderDetailVO;
import com.scau402.system1.mapper.IWmsAdjustBoundOrderDetailMapper;
import com.scau402.system1.repository.IWmsAdjustDetailRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 库存调整单详情Repository实现类
 * @since 2024/2/3
 */
@Repository
@Slf4j
public class WmsAdjustDetailRepositoryImpl implements IWmsAdjustDetailRepository {
    @Autowired
    private IWmsAdjustBoundOrderDetailMapper adjustDetailMapper;

    @Override
    public void saveList(List<WmsAdjustBoundOrderDetailPO> orderDetailList) {
        adjustDetailMapper.saveBatch(orderDetailList);
    }

    @Override
    public List<WmsAdjustBoundOrderDetailVO> findListByAdjustOrderId(Long adjustOrderId) {
        return adjustDetailMapper.selectAdjustOrderDetailById(adjustOrderId);
    }

    /**
     * 根据审核单id获取
     * @param auditId 审核单id
     * @return
     */
    @Override
    public WmsAdjustBoundOrderDetailPO find(Long auditId) {
        adjustDetailMapper.selectOne(new QueryWrapper<WmsAdjustBoundOrderDetailPO>().eq("audit_id",auditId));
        return null;
    }

    /**
     * 根据审核单id获取对应的调整详情列表
     * @param poList 审核单PO列表
     * @return 调整详情列表
     */
    @Override
    public List<WmsAdjustBoundOrderDetailPO> findAdjustList(List<WmsAuditOrderPO> poList) {
        if (poList.isEmpty()) {
            return new ArrayList<>();
        }
        List<Long> ids = poList.stream().map(WmsAuditOrderPO::getId).collect(Collectors.toList());
        return adjustDetailMapper.selectList(new QueryWrapper<WmsAdjustBoundOrderDetailPO>().in("audit_id", ids));
    }

    /**
     * 根据审核单id获取对应的调整详情列表
     * @param adjustBoundOrderQuery 审核单
     * @return 调整详情列表
     */
    @Override
    public WmsAdjustBoundOrderDetailPO findAdjustByAudit(WmsAdjustBoundOrderQuery adjustBoundOrderQuery){
        if (adjustBoundOrderQuery == null||adjustBoundOrderQuery.getAuditId()==null) {
            return null;
        }
        return adjustDetailMapper.selectOne(new QueryWrapper<WmsAdjustBoundOrderDetailPO>()
                .eq("audit_id",adjustBoundOrderQuery.getAuditId())
                .eq(adjustBoundOrderQuery.getBarcode()!=null,"barcode",adjustBoundOrderQuery.getBarcode()));
    }

    @Override
    public WmsAdjustBoundOrderDetailPO findAdjustByAudit(WmsAuditOrderPO po) {
        return null;
    }


}
